package ro.dandanciu.webgallery.ui.general;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import ro.dandanciu.webgallery.domainmodel.Gallery;
import ro.dandanciu.webgallery.domainmodel.Roles;
import ro.dandanciu.webgallery.domainmodel.User;
import ro.dandanciu.webgallery.services.GalleryServices;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Base Action class for the Tutorial package.
 *
 * RFC: make public galleries part viewable without need from each
 */
@SuppressWarnings("serial")
public class WebGalleryAction extends ActionSupport implements
        ServletRequestAware, SessionAware {

    protected static final String MESSAGE = "message";

    private Map<String, Object> session = new HashMap<String, Object>();

    private HttpServletRequest request;

    private Set<Roles> requiredRoles = new TreeSet<Roles>();

    // TODO remove the need for this
    protected GalleryServices galleryServices = null;

    @SuppressWarnings("unchecked")
    public void setSession(final Map session) {
        this.session = session;
    }

    protected User getSessionUser() {
        if (session == null) {
            return null;
        }
        return (User) session.get(Constants.SESSION_USER_KEY);
    }

    protected void setSessionUser(final User user) {
        if (session != null) {
            session.put(Constants.SESSION_USER_KEY, user);

            final String language = user.getLanguage();
            if (language != null) {
                final Locale locale = new Locale(language, "");
                session.put("request_locale", locale);
            }
        }
    }

    protected void removeSessionUser() {
        if (session.containsKey(Constants.SESSION_USER_KEY)) {
            session.remove(Constants.SESSION_USER_KEY);
        }
    }

    protected boolean isSessionValid() {
        return (session != null) && (getSessionUser() != null);
    }

    protected String setMessage(final String key) {
        request.setAttribute("message.key", key);
        return MESSAGE;
    }

    public void setServletRequest(final HttpServletRequest request) {
        this.request = request;

    }

    public final Set<Roles> getRequiredRoles() {
        return requiredRoles;
    }

    public final void setRequiredRoles(final Set<Roles> requiredRoles) {
        this.requiredRoles = requiredRoles;
    }

    public List<Gallery> getPublicGalleries() {
        return galleryServices.getPublicGalleries("");
    }

    public GalleryServices getGalleryServices() {
        return galleryServices;
    }

    public void setGalleryServices(final GalleryServices galleryServices) {
        this.galleryServices = galleryServices;
    }
}
